<template>
  <page-header-wrapper :title="false">
    <a-card class="content-card">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="姓名">
                <a-input v-model="queryParam.name" placeholder="姓名" allowClear />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="联系电话">
                <a-input v-model="queryParam.tel" placeholder="联系电话" allowClear />
              </a-form-item>
            </a-col>
            <a-col :md="!advanced && 6 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="getSearch">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => this.queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <a-table 
        size="middle"
        :columns="columns" 
        :data-source="data" 
        :rowKey="r=>r.id"
        :pagination="pagination"
        :loading="loading"
        :scroll="{x: _width}"
      >
        <template slot="action" slot-scope="r">
          <a-popconfirm
            title="您确定要删除吗?"
            ok-text="确定"
            cancel-text="取消"
            @confirm="handleDeleteClick(r)"
          >
            <a>删除</a>
          </a-popconfirm>
        </template>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { toggleQuery,showMessage } from '@/utils/mixins'
import { getMessageList,deleteMessageList } from '@/api/manage'

export default {
  name: 'LeaveMessage',
  mixins: [toggleQuery,showMessage],
  data () {
    return {
      queryParam: {},
      columns: [
        {title: '序号',customRender: (t,r,i) => `${i+1}`,width: 70,align: 'center'},
        {title: '姓名',dataIndex: 'name',key: 'name',width: 120},
        {title: '联系电话',dataIndex: 'tel',key: 'tel',width: 140},
        {title: '邮箱',dataIndex: 'email',key: 'email',width: 160},
        {title: '地址',dataIndex: 'address',key: 'address',width: 400,ellipsis: true},
        {title: '留言内容',dataIndex: 'content',key: 'content',width: 700,ellipsis: true},
        {title: '操作',scopedSlots: {customRender: 'action'},width: 120,align: 'center',fixed: 'right'}
      ],
      data: [],
      loading: false,
      pagination: {
        total: 0,
        pageSize: 25,
        showTotal: total => `共 ${total} 条`
        // showSizeChanger: true,
        // pageSizeOptions: ['10', '20', '30', '40']
      },
      pageNo: 1,
    }
  },
  computed: {
    _width() {
      return this.columns.reduce((x,y) => {return x + y.width},0)
    }
  },
  methods: {
    // 分页查询
    paginationChange(e) {
      this.pageNo = e.current
      this.pageSize = e.pageSize
      this.getList()
    },
    // 查询
    getSearch() {
      this.pageNo = 1
      this.getList()
    },
    // 删除
    handleDeleteClick(r) {
      deleteMessageList({id: r.id}).then(res => {
        if(res.code == '0') {
          this.showMessage(res,this.getList)
        }
      })
    },
    // 获取列表
    getList() {
      this.loading = true
      getMessageList({
        pageNo: this.pageNo,
        ...this.queryParam
      }).then(res => {
        this.loading = false
        if(res.code == '0') {
          this.data = res.data.result
          this.pagination.total = res.data.count
        }
      })
    }
  },
  mounted() {
    this.getList()
  }
}
</script>